package icdecs.action.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;

public class RegisListActionForm extends DynaValidatorForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		ActionErrors errors = super.validate(mapping, request);
		FormFile uploadFile = (FormFile) this.get("uploadFile");
		String cardID = (String) this.get("cardID");
		String mode = (String) this.get("mode");
		if (mode.compareToIgnoreCase("csv") == 0) {
			if (uploadFile == null || uploadFile.getFileSize() == 0) {
				errors.add("common.file.err", new ActionMessage(
						"error.common.file.required"));
				return errors;
			}

			// only allow textfile to upload
			if (!"text/plain".equals(uploadFile.getContentType())) {
				errors.add("common.file.err.ext", new ActionMessage(
						"error.common.file.textfile.only"));
				return errors;
			}

		} else {
			if (cardID.trim().compareToIgnoreCase("") == 0) {
				errors.add("errors.required", new ActionMessage(
						"error.required.carid"));
				return errors;
			}
		}

		return errors;

	}

}
